package Array;

import java.util.Arrays;

public class myArray {
    private int[] data;
    private int size;
    public myArray(){
        data = new int[10];
    }
    public myArray(int num){
        data = new int[num];
    }
    public String toString(){
        String ret = "[";
        for (int i = 0; i < size; i++) {
            ret += data[i];;
            if(i != size - 1) {
                ret += ",";
            }
        }
        ret += "]";
        return ret;
    }
    //扩容
    private void grow(){
        int[] newData = Arrays.copyOf(this.data,this.data.length << 1);
        this.data = newData;
    }
    //增加
    public void addIndex(int index,int value){
        if(0 > index || index >= size) {
            System.err.println("输入有误!");
            return;
        }
        if(size == data.length){
            grow();
        }
        if(index == 0){
            addFirst(value);
        }
        if(index == size){
            addLast(value);
        }
        for (int i = size - 1; i >= index; i--) {
            data[i + 1] = data[i];
        }
        data[index] = value;
        size++;
    }
    public void addFirst(int value){
        if(size == data.length){
            grow();
        }
        for (int i = size - 1; i >= 0; i--) {
            data[i + 1] = data[i];
        }
        data[0] = value;
        size++;
    }
    public void addLast(int value){
        if(size == data.length){
            grow();
        }
        data[size] = value;
        size++;
    }
//删除
    public void dataDeleteIndex(int index){
        if(0 > index || index > size){
            System.err.println("输入有误!");
            return;
        }
        for (int i = index; i < data.length - 1; i++) {
            data[i] = data[i + 1];
        }
        data[size] = 0;
        size--;
    }
    public void dataDeleteAll(int value){
        for (int i = 0; i < data.length; i++) {
            while(data[i] == value){
                dataDeleteIndex(i);
            }
            if(size == data.length){
                break;
            }
        }
    }
    //修改
    public void dataRevise(int index,int value){
        if(0 > index || index > size){
            System.err.println("输入有误!");
            return;
        }
        data[index] = value;
    }
    //查看
    public void dataView(int value){
        boolean falg = false;
        for (int i = 0; i < data.length; i++) {
            if(data[i] == value){
                System.out.print("找到了!");
                System.out.println("下标为" + i);
                falg = true;
            }
        }
        if(falg == false){
            System.out.println("没找到!!");
        }
    }
    public int  dataViewIndex(int index){
        if(0 > index || index > size){
            System.err.println("输入有误!");
            return -1;
        }
        return data[index];
    }
}
